package goods

import (
	"fmt"
	// "log"
	"server/conf"
	"server/lib/mysqldb"
	"strconv"
)

func Format(uid string, page string) (map[int]map[string]string, string) {
	db := mysqldb.GetConnect()

	start, _ := strconv.Atoi(page)
	end, _ := strconv.Atoi(conf.GET["app"]["admin_page"])
	where := fmt.Sprintf("a.uid = %s", uid)

	db.SetTable("i_goods_format a").Where(where).Limit((start-1)*10, end)
	db.LeftJoin("i_goods_class b", "a.classid = b.id")
	db.Fileds("a.*", "b.name classname")

	data := db.FindAll()
	total := db.SetTable("i_goods_format").Fileds("count(1) c").FindAll()
	return data, total[1]["c"]
}

func AddFormat(param map[string]interface{}) {
	db := mysqldb.GetConnect()

	db.SetTable("i_goods_format").Insert(param)
}

func DelFormat(id, uid string) string {
	db := mysqldb.GetConnect()

	where := "uid = %s AND id = %s"
	db.SetTable("i_goods_format").Delete(fmt.Sprintf(where, uid, id))

	return "1"
}

func UpdateFormat(uid, id string, param map[string]interface{}) (int, error) {
	db := mysqldb.GetConnect()
	return db.SetTable("i_goods_format").Where("id = '" + id + "' and uid = '" + uid + "'").Update(param)
}

func Formatinfo(uid, id string) map[int]map[string]string {
	db := mysqldb.GetConnect()

	ret := db.SetTable("i_goods_format").Where("uid = '" + uid + "' and id = '" + id + "'").FindOne()

	return ret
}

func GetFormat(uid, classid string) map[int]map[string]string {
	db := mysqldb.GetConnect()

	ret := db.SetTable("i_goods_format").Where("uid = '" + uid + "' and classid = '" + classid + "'").FindAll()

	return ret
}
